package com.soft.face.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.soft.face.entity.ArcSoft;
import com.soft.face.entity.dto.ArcSoftDTO;
import com.soft.face.entity.dto.ArcSoftInsertDTO;
import com.soft.face.global.BusinessException;
import com.soft.face.global.R;
import com.soft.face.service.ArcSoftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "虹软")
@RestController
@RequestMapping("arcsoft")
@AllArgsConstructor
public class ArcSoftController {

    private ArcSoftService arcSoftService;

    @PostMapping("/insert")
    @ApiOperation(value = "虹软数据添加")
    public R<?> insert(ArcSoftInsertDTO dto) throws Exception {
        if (dto.getLibarcsoftFace() == null) {
            throw new BusinessException("LibarcsoftFace不能为空");
        }
        if (dto.getSimple() == null) {
            throw new BusinessException("数据样本文件不能为空");
        }
        if (dto.getLibarcsoftFaceEngine() == null) {
            throw new BusinessException("LibarcsoftFaceEngine文件不能为空");
        }
        if (dto.getLibarcsoftFaceEngineJni() == null) {
            throw new BusinessException("LibarcsoftFaceEngineJni文件不能为空");
        }
        if (StringUtils.isEmpty(dto.getVersion())) {
            throw new BusinessException("版本号不能为空");
        }
        if (StringUtils.isEmpty(dto.getAppId())) {
            throw new BusinessException("AppId不能为空");
        }
        if (StringUtils.isEmpty(dto.getAppKey())) {
            throw new BusinessException("AppKey不能为空");
        }
        arcSoftService.insert(dto);
        return R.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "虹软数据列表")
    public R<PageInfo<ArcSoft>> list(@Valid ArcSoftDTO dto) {
        return R.success(arcSoftService.list(dto));
    }

    @PostMapping("/task")
    @ApiOperation(value = "数据测试")
    public R<?> task(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空");
        }
        return R.success(arcSoftService.task(id));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "虹软数据删除")
    public R<?> delete(String id) throws BusinessException {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空");
        }
        arcSoftService.delete(id);
        return R.success();
    }

}
